package dbms;

import java.util.Arrays;

public class DTD {

	public String getTableName(String s){
		StringBuilder sb=new StringBuilder("");
		int i=10;
		while(s.charAt(i)!=' ')
			sb.append(s.charAt(i++));
		return sb.toString();
	}
	
	public String [] getColumnNames(String s)
	{
		String sb="";
		int i=s.length()-2;
		while(s.charAt(i)!='(')
		{
			sb=s.charAt(i--)+sb;
		}
		return sb.split(",");
		
	}
	
	public String getColumnType(String s) {
		int i = s.length() - 3;
		String ans = "";
		while (s.charAt(i) != '"') {
			ans = s.charAt(i) + ans;
			i--;
		}
		return ans;
	}
	
	public String tableLine(String s)
	{
		return "<!DOCTYPE "+s+" [";
	}
	
	public String setColums(String s,String tableName)
	{
		return "<!ELEMENT "+tableName+" "+s+">";
	}
	
	public String columnEleAtt(String type,String nullable,String columnName)
	{
		String st="<!ELEMENT "+columnName+">"+"\n";
		String st2="<!ATTLIST "+columnName+" Type"+" CDATA "+"\""+type+ "\"" +">"+"\n";
		return st+st2+"<!ATTLIST "+columnName+" IsBoolean"+" CDATA "+"\""+nullable+ "\"" +">";
	}
	public String deleteTable(String s,String name)
	{
		String st[]=s.split("]>");
		
		StringBuilder ans=new StringBuilder();
		for (int i = 0; i < st.length-1; i++) {
			if(!st[i].contains(name)&&st[i]!=null&&!st[i].isEmpty())
			{
				
				ans.append(st[i]+"\n]>");
			}
		}
		
		return  ans.toString();
	}
	
	public String getTable(String s,String name)
	{
		String st[]=s.split("]>");
		StringBuilder ans=new StringBuilder();
		for (int i = 0; i < st.length; i++) {
			if(st[i].contains(name))
			{
				
				ans.append(st[i]+"]>");
			}
		}
		if((ans.charAt(0)+"").equals("\n"))
			return ans.substring(1,ans.length()-1);
		else
			return  ans.toString();
	}
	public static void main(String[] args) {
		DTD d=new DTD();
		String st=d.getTableName("<!DOCTYPE BOOKSTORE [");
		System.out.println(st.length());
		System.out.println(Arrays.toString(d.getColumnNames("!<ELEMENT BOOKSTORE (BOOK,TITLE,AUTHOR)")));
		System.out.println((d.getColumnType("!<ATTLIST BOOK cdata \"kza\">")));
		String sx="<!DOCTYPE bookstor [\n<!ELEMENT boohstore (book,Title,Author)>\n<!ELEMENT book>]>\n<!DOCTYPE bookstore [\n<!ELEMENT boohstore (book,Title,Author)>\n<!ELEMENT book>\n]>";
		System.out.println(d.getTable(sx, "bookstore"));
	}

}
